package com.yh.autobio.modules.adatper;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.support.design.widget.Snackbar;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yh.autobio.R;
import com.yh.autobio.common.ArithUtil;
import com.yh.autobio.common.StringUtils;
import com.yh.autobio.component.dialog.DialogHelp;
import com.yh.autobio.gddb.gbmanager.MatCountManager;
import com.yh.autobio.gddb.gbmanager.ScoreEntryManager;
import com.yh.autobio.gddb.gdbean.MatBeanCount;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by imyar on 2016/5/3 0031.
 */
public class Sale_AddAdapter extends RecyclerView.Adapter<Sale_AddAdapter.SaleViewHolder> {

    private Context mContext;
    private ArrayList<MatBeanCount> workInfos;
    private String ftype;
    private String Show_type;

    public Sale_AddAdapter(Context context, ArrayList<MatBeanCount> workInfos, OnSaleAddAdapterListern listener, String ftype) {
        mContext = context;
        this.workInfos = workInfos;
        this.listener = listener;
        this.ftype = ftype;
    }

    public Sale_AddAdapter(Context context, ArrayList<MatBeanCount> workInfos, OnSaleAddAdapterListern listener, String ftype,String Show_type) {
        mContext = context;
        this.workInfos = workInfos;
        this.listener = listener;
        this.ftype = ftype;
        this.Show_type = Show_type;
    }

    public List<MatBeanCount> getListMatBean() {
        return workInfos;
    }

    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

    @Override
    public SaleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_sale_out, parent,
                false);
        return new SaleViewHolder(view);
    }


    @Override
    public void onBindViewHolder(final SaleViewHolder holder, final int position) {

        if (holder instanceof SaleViewHolder) {
            final MatBeanCount bean = workInfos.get(position);
            if ("0".equals(ftype)) {
                holder.tv_sales_money.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                    }

                    @Override
                    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                    }

                    @Override
                    public void afterTextChanged(Editable editable) {
//                        String trim = editable.toString().trim();
//                        double d;
//                        try {
//                            double v1 = Double.parseDouble(trim);
//                            d = v1;
//                        } catch (Exception e) {
//                            d = 0;
//                            holder.tv_sales_money.setText("0");
//                        }
//
//                        bean.setAllMoney(d);
                        if (listener != null) {
                            listener.onMoneyChanged();
                        }
                    }
                });
                holder.ll_xsdj.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        final View view1 = View.inflate(mContext, R.layout.dialog_change_mat, null);
                        final TextView title = (TextView) view1.findViewById(R.id.tv_title);
                        final EditText content = (EditText) view1.findViewById(R.id.et_content);
                        content.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
                        title.setText(bean.getName() + " -- " + "销售单价");
                        String s = holder.tv_sales_single.getText().toString().trim();
                        if (!StringUtils.isEmpty(s)){
                            content.setText(s);
                            content.setSelection(s.length());
                        }
                        DialogHelp.getConfirmDialog(mContext, view1, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                String trim = content.getText().toString().toString();
                                double price;
                                if (!".".equals(trim)) {
                                    if (StringUtils.isEmpty(trim)) {
                                        price = 0;
                                    } else {
                                        price = Double.parseDouble(trim);
                                    }
                                    bean.setSaleprice(price);
                                    double round = ArithUtil.round(Integer.parseInt(bean.getCount()) * price, 0);
                                    bean.setAllMoney(round);
                                    holder.tv_sales_money.setText(round + "");
                                    holder.tv_sales_single.setText(price + "");
                                } else {
                                    bean.setSaleprice(0);
                                    holder.tv_sales_single.setText("0");
                                    bean.setAllMoney(0);
                                    holder.tv_sales_money.setText("0");
                                    holder.tv_sales_single.setText("0");
                                }
                            }
                        }).show();
                    }
                });
                holder.ll_xsje.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        final View view1 = View.inflate(mContext, R.layout.dialog_change_mat, null);
                        final TextView title = (TextView) view1.findViewById(R.id.tv_title);
                        final EditText content = (EditText) view1.findViewById(R.id.et_content);
                        content.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
                        title.setText(bean.getName() + " -- " + "销售金额");
                        String s = holder.tv_sales_money.getText().toString().trim();
                        if (!StringUtils.isEmpty(s)){
                            content.setText(s);
                            content.setSelection(s.length());
                        }
                        DialogHelp.getConfirmDialog(mContext, view1, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                String trim = content.getText().toString().toString();
                                double d;
                                try {
                                    double v1 = Double.parseDouble(trim);
                                    d = v1;
                                } catch (Exception e) {
                                    d = 0;
                                    bean.setAllMoney(0);
                                    holder.tv_sales_money.setText("0");
                                }
                                bean.setAllMoney(d);
                                holder.tv_sales_money.setText(d+"");
//                                if (listener != null) {
//                                    listener.onMoneyChanged();
//                                }
                            }
                        }).show();
                    }
                });

                holder.ll_sfsl.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        final View view1 = View.inflate(mContext, R.layout.dialog_change_mat, null);
                        final TextView title = (TextView) view1.findViewById(R.id.tv_title);
                        final EditText content = (EditText) view1.findViewById(R.id.et_content);
//                content.setInputType(InputType.TYPE_CLASS_NUMBER);
                        title.setText(bean.getName() + " -- " + "实发数量");
                        String s = holder.tv_sf_num.getText().toString().trim();
                        if (!StringUtils.isEmpty(s)) {
                            String[] split = s.split("=");
                            String s1 = split[0];
                            content.setText(s1);
                            content.setSelection(s1.length());
                        }
                        DialogHelp.getConfirmDialog(mContext, view1, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                String trim = content.getText().toString().toString();

                                if (StringUtils.isEmpty(trim)) {
                                    holder.tv_sf_num.setText("0");
                                    trim = "0";
                                }
//                                if (listener != null) {
//                                    listener.onCountChanger(bean.getId(), trim);
//                                }
                                if (StringUtils.isEmpty(trim)) {
                                    holder.tv_sales_money.setText("0");
                                    workInfos.get(position).setCount("0");
                                    bean.setAllMoney(0);
                                } else {
                                    try {
//                                        double count3 = StringUtils.eval(trim);
//                                        String count2 = StringUtils.subZeroAndDot(count3 + "");
//                                        int count = Integer.parseInt(count2);
                                        double count = StringUtils.eval(trim);
                                        workInfos.get(position).setCount(StringUtils.subZeroAndDot(count + "") + "");
                                        double v = ArithUtil.round((bean.getSaleprice()) * count, 0);
                                        holder.tv_sales_money.setText(v + "");
                                        bean.setAllMoney(v);

                                        if ("1".equals(ftype)) {//计算赠品积分
                                            String credit = bean.getCredit();
                                            if (StringUtils.isEmpty(credit)) {
                                                credit = "0";
                                            }
                                            int jf = Integer.parseInt(credit) * (Integer.parseInt(bean.getCount()));
                                            if (0 == jf) {
                                                holder.tv_jf.setText("0");
                                            } else {
                                                holder.tv_jf.setText(jf + "");
                                            }
                                            bean.setFUnitTradeScore(credit);
                                            bean.setFTradeScore(jf + "");
                                        }
                                        if ("0".equals(ftype)) {//计算商品
                                            int count1 = Integer.parseInt(bean.getCount());
                                            double scoreByItemId = ScoreEntryManager.getInstance(mContext).getScoreByItemId(bean.getId(), count1);
                                            if (0 == scoreByItemId) {
                                                holder.tv_jf.setText("0");
                                            } else {
                                                holder.tv_jf.setText(scoreByItemId * count1 + "");
                                            }
                                            bean.setFUnitScore(scoreByItemId + "");
                                            bean.setFItemScore(scoreByItemId * count1 + "");
                                        }
                                        holder.tv_sf_num.setText(trim + "=" + StringUtils.subZeroAndDot(count + ""));
                                    } catch (Exception e) {
                                        Snackbar.make(((Activity) mContext).getWindow().getDecorView(), "输入公式格式错误，请重新输入", Snackbar.LENGTH_SHORT).show();
                                    }

                                }
                            }
                        }).show();
                    }
                });

            } else {
                holder.tv_sf_num.setEnabled(false);
                holder.tv_sales_single.setEnabled(false);
                holder.tv_sales_money.setEnabled(false);
            }

//            holder.tv_sf_num.addTextChangedListener(new TextWatcher() {
//                @Override
//                public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
//
//                }
//
//                @Override
//                public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
//
//                }
//
//                @Override
//                public void afterTextChanged(Editable editable) {
//                    String trim = editable.toString();
//                    if (StringUtils.isEmpty(trim)) {
//                        holder.tv_sf_num.setText("0");
//                        trim = "0";
//                    }
//                    if (listener != null) {
//                        listener.onCountChanger(bean.getId(), trim);
//                    }
//                    if (StringUtils.isEmpty(trim)) {
//                        holder.tv_sales_money.setText("0");
//                        workInfos.get(position).setCount("0");
//                        bean.setAllMoney(0);
//                    } else {
//                        int count = Integer.parseInt(trim);
//                        double v = ArithUtil.round((bean.getSaleprice()) * count, 0);
//                        holder.tv_sales_money.setText(v + "");
//                        bean.setAllMoney(v);
//                        workInfos.get(position).setCount(count + "");
//
//                        if ("1".equals(ftype)) {//计算赠品积分
//                            String credit = bean.getCredit();
//                            if (StringUtils.isEmpty(credit)) {
//                                credit = "0";
//                            }
//                            int jf = Integer.parseInt(credit) * (Integer.parseInt(bean.getCount()));
//                            if (0 == jf) {
//                                holder.tv_jf.setText("0");
//                            } else {
//                                holder.tv_jf.setText(jf + "");
//                            }
//                            bean.setFUnitTradeScore(credit);
//                            bean.setFTradeScore(jf + "");
//                        }
//                        if ("0".equals(ftype)) {//计算商品
//                            int count1 = Integer.parseInt(bean.getCount());
//                            double scoreByItemId = ScoreEntryManager.getInstance(mContext).getScoreByItemId(bean.getId(), count1);
//                            if (0 == scoreByItemId) {
//                                holder.tv_jf.setText("0");
//                            } else {
//                                holder.tv_jf.setText(scoreByItemId * count1 + "");
//                            }
//                            bean.setFUnitScore(scoreByItemId + "");
//                            bean.setFItemScore(scoreByItemId * count1 + "");
//                        }
//                    }
//                }
//            });
            holder.ll_fhck.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (listener != null) {
                        listener.onFHCKClick(bean, holder.getAdapterPosition());
                    }
                }
            });
//            holder.tv_note.addTextChangedListener(new TextWatcher() {
//                @Override
//                public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
//
//                }
//
//                @Override
//                public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
//
//                }
//
//                @Override
//                public void afterTextChanged(Editable editable) {
//                    String note = editable.toString().trim();
//                    bean.setFNote(note + "");
//                }
//            });
            holder.ll_bz.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    final View view1 = View.inflate(mContext, R.layout.dialog_change_mat, null);
                    final TextView title = (TextView) view1.findViewById(R.id.tv_title);
                    final EditText content = (EditText) view1.findViewById(R.id.et_content);
                    content.setInputType(InputType.TYPE_CLASS_TEXT);
                    title.setText(bean.getName() + " -- " + "备注说明");
                    String s = holder.tv_note.getText().toString();
                    if (!StringUtils.isEmpty(s)){
                        content.setText(s);
                        content.setSelection(s.length());
                    }
                    DialogHelp.getConfirmDialog(mContext, view1, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            String trim = content.getText().toString().toString();
                            bean.setFNote(trim);
                            holder.tv_note.setText(trim);
                        }
                    }).show();
                }
            });
//            String value = MatHeadManager.getInstance(mContext).getHeadValue("发货仓库","2");


            String value = bean.getTDCStockName();
            String cusvalue = bean.getFDCStockName();//表头发货仓库
            if (!StringUtils.isEmpty(value)) {
                holder.tv_fh_ware.setText(value);//发货仓库
            } else {
                holder.tv_fh_ware.setText(cusvalue);//发货仓库
            }
            if ("1".equals(ftype)) {//计算赠品积分
                String credit = bean.getCredit();
                if (StringUtils.isEmpty(credit)) {
                    credit = "0";
                }
                int jf = Integer.parseInt(credit) * (Integer.parseInt(bean.getCount()));
                if (0 == jf) {
                    holder.tv_jf.setText("0");

                } else {
                    holder.tv_jf.setText(jf + "");
                }
            }
            if ("0".equals(ftype)) {//计算商品
                int count = Integer.parseInt(bean.getCount());
                double scoreByItemId = ScoreEntryManager.getInstance(mContext).getScoreByItemId(bean.getId(), count);
                if (0 == scoreByItemId) {
                    holder.tv_jf.setText("0");
                } else {
                    holder.tv_jf.setText(scoreByItemId * count + "");
                }
            }
            holder.tv_cc_code.setText(bean.getNumber());
            holder.tv_sf_num.setText(bean.getCount());

            holder.tv_gg_modle.setText(bean.getModel());
//            holder.tv_note.setText(bean.);//备注
            double v = (bean.getSaleprice()) * Integer.parseInt(bean.getCount());
            bean.setAllMoney(ArithUtil.round(v, 0));
            double qty = bean.getQty();
            holder.tv_jskc.setText(qty+"");
            holder.tv_sales_money.setText(ArithUtil.round(v, 0) + "");
            holder.tv_sales_single.setText(bean.getSaleprice() + "");
            holder.tv_unit.setText(bean.getUnit_name());
            holder.tv_cc_name.setText(bean.getName());
            if (StringUtils.isEmpty(bean.getFNote())) {
                holder.tv_note.setText("");
            } else {
                holder.tv_note.setText(bean.getFNote());
            }
//            holder.tv_sales_single.addTextChangedListener(new TextWatcher() {
//                @Override
//                public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
//
//                }
//
//                @Override
//                public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
//
//                }
//
//                @Override
//                public void afterTextChanged(Editable editable) {
//                    String trim = editable.toString().trim();
//                    double price;
//                    if (!".".equals(trim)) {
//                        if (StringUtils.isEmpty(trim)) {
//                            price = 0;
//                        } else {
//                            price = Double.parseDouble(trim);
//                        }
//                        bean.setSaleprice(price);
//                        double round = ArithUtil.round(Integer.parseInt(bean.getCount()) * price, 0);
//                        holder.tv_sales_money.setText(round + "");
//                        bean.setAllMoney(round);
//                    } else {
//                        bean.setSaleprice(0);
//                        holder.tv_sales_single.setText("0");
//                        holder.tv_sales_money.setText("0");
//                        bean.setAllMoney(0);
//                    }
//                }
//            });

            ((SaleViewHolder) holder).itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View view) {
                    DialogHelp.getConfirmDialog(mContext, "确定删除  " + bean.getName() + "  么？", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            onItemDismiss(holder.getAdapterPosition(),bean);
                        }
                    }).show();
                    return true;
                }
            });
        }
    }

    private OnSaleAddAdapterListern listener;


    public void onItemDismiss(int position,MatBeanCount bean) {
        if ("show".equals(Show_type)){
            MatCountManager.getInstance(mContext).delMat(bean);//提交成功后删除缓存
        }
        //删除mItems数据
        workInfos.remove(position);
        //删除RecyclerView列表对应item
        notifyItemRemoved(position);
    }

    public interface OnSaleAddAdapterListern {
        void onFHCKClick(MatBeanCount bean, int position);

        void onCountChanger(String id, String count);

        void onMoneyChanged();
    }

    @Override
    public int getItemCount() {
        return workInfos.size();
    }


    public class SaleViewHolder extends RecyclerView.ViewHolder {

        private TextView tv_cc_code;
        private TextView tv_cc_name;
        private TextView tv_gg_modle;
        private TextView tv_unit;
        private TextView tv_sf_num;
        private TextView tv_sales_single;
        private TextView tv_sales_money;
        private TextView tv_fh_ware;
        private TextView tv_jf;
        private TextView tv_note;
        private TextView tv_jskc;
        private LinearLayout ll_fhck;
        private LinearLayout ll_xsdj;
        private LinearLayout ll_xsje;
        private LinearLayout ll_sfsl;
        private LinearLayout ll_bz;
        private LinearLayout ll_jskc;


        public SaleViewHolder(View view) {
            super(view);
            tv_cc_code = (TextView) view.findViewById(R.id.tv_cc_code);
            tv_cc_name = (TextView) view.findViewById(R.id.tv_cc_name);
            tv_gg_modle = (TextView) view.findViewById(R.id.tv_gg_modle);
            tv_unit = (TextView) view.findViewById(R.id.tv_unit);
            tv_sf_num = (TextView) view.findViewById(R.id.tv_sf_num);
            tv_sales_single = (TextView) view.findViewById(R.id.tv_sales_single);
            tv_sales_money = (TextView) view.findViewById(R.id.tv_sales_money);
            tv_fh_ware = (TextView) view.findViewById(R.id.tv_fh_ware);
            tv_jf = (TextView) view.findViewById(R.id.tv_position);
            tv_note = (TextView) view.findViewById(R.id.tv_note);
            tv_jskc = (TextView) view.findViewById(R.id.tv_jskc);
            ll_fhck = (LinearLayout) view.findViewById(R.id.ll_fhck);
            ll_xsdj = (LinearLayout) view.findViewById(R.id.ll_xsdj);
            ll_xsje = (LinearLayout) view.findViewById(R.id.ll_xsje);
            ll_sfsl = (LinearLayout) view.findViewById(R.id.ll_sfsl);
            ll_bz = (LinearLayout) view.findViewById(R.id.ll_bz);
            ll_jskc = (LinearLayout) view.findViewById(R.id.ll_jskc);

        }
    }
}
